/*
* @Author: haodaquan
* @Date:   2017-06-20 10:01:39
* @Last Modified by:   haodaquan
* @Last Modified time: 2017-06-20 10:02:07
 */

package libs

import (
	"crypto/md5"
	"fmt"
	"regexp"

	"github.com/google/uuid"
)

var emailPattern = regexp.MustCompile("[\\w!#$%&'*+/=?^_`{|}~-]+(?:\\.[\\w!#$%&'*+/=?^_`{|}~-]+)*@(?:[\\w](?:[\\w-]*[\\w])?\\.)+[a-zA-Z0-9](?:[\\w-]*[\\w])?")

func Md5(buf []byte) string {
	hash := md5.New()
	hash.Write(buf)
	return fmt.Sprintf("%x", hash.Sum(nil))
}

func SizeFormat(size float64) string {
	units := []string{"Byte", "KB", "MB", "GB", "TB"}
	n := 0
	for size > 1024 {
		size /= 1024
		n += 1
	}

	return fmt.Sprintf("%.2f %s", size, units[n])
}

func IsEmail(b []byte) bool {
	return emailPattern.Match(b)
}

func UUID() string {
	return uuid.New().String()
}

func GetMTodoTitleByType(i int) string {
	switch i {
	case 0:
		return "待收件"
	case 1:
		return "检验中"
	case 2:
		return "鉴定中"
	case 3:
		return "待取件"
	case 4:
		return "已取件"
	default:
		return ""
	}
}

func GetUTodoTitleByType(i int) string {
	switch i {
	case 0:
		return "待送件"
	case 1:
		return "待取件"
	case 2:
		return ""
	default:
		return ""
	}

}

func GetTopStr(i int) string {
	switch i {
	case 0:
		return "不予受理"
	case 1:
		return "中止检验"
	case 2:
		return "终止检验"
	case 3:
		return "补充送检"
	default:
		return ""
	}
}

func GetProjectType(rtype int) string {
	switch rtype {
	case 0:
		return "未上报"
	case 1:
		return "立项审核中"
	case 2:
		return "项目已启动"
	case 3:
		return "中期审核中"
	case 4:
		return "已通过中期审核"
	case 5:
		return "结题审核中"
	case 6:
		return "项目已结题"
	case 7:
		return "成果物交付中"
	case 8:
		return "成果物已交付"
	default:
		return ""
	}
}

func GetImsSequenceParamsNameByType(rtype int) string {
	switch rtype {
	case 0:
		return "申报截止日期"
	case 1:
		return "中期检查截止日期"
	case 2:
		return "项目结题截止日期"
	case 3:
		return "其它"
	default:
		return "其它"
	}
}

func GetBusyName(rtype int) string {
	switch rtype {
	case 0:
		return "ims_project"
	case 1:
		return "ims_project_medium_term"
	case 2:
		return "ims_project_finish_report"
	case 3:
		return "ims_project_flow"
	default:
		return ""
	}
}

func GetImsProjectType(rtype int) string {
	switch rtype {
	case 0:
		return "创新训练项目"
	case 1:
		return "创业训练项目"
	case 2:
		return "创业实践项目"
	default:
		return ""
	}
}
